package org.example.util;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.security.Key;
import java.util.Date;

@Component
public class FmBackendJwtUtils {
    public final static String ACCESS_TOKEN_KEY="access_token";
    public final static String REFRESH_TOKEN_KEY="refresh_token";
    private Key accessKey;
    private Key refreshKey;
    private long accessJwtExpire;
    private long refreshJwtExpire;

    public String createAccessToken(String username){
        return createToken(username,accessJwtExpire,accessKey);
    }
    public String createRefreshToken(String username){
        return createToken(username,refreshJwtExpire,refreshKey);

    }
    /**
     * 创建token
     * @param username 用户名
     * @param expire  jwt的过期时间
     * @param key 是用来签名的，就是用来封装签名字符串
     * @return
     */
    private String createToken(String username, long expire, Key key){
        return Jwts.builder()
                .setSubject(username)//设置当前jwt用于谁
                .setIssuedAt(new Date())//设置Jwt的创建时间
                .setExpiration(new Date(System.currentTimeMillis()+expire))//设置过期时间
                .signWith(key, SignatureAlgorithm.HS512)
                .compact();//返回签名对应的字符串
    }
}
